//
//  ComposeViewController.swift
//  SinaWeibo
//
//  Created by liyue on 15/11/21.
//  Copyright © 2015年 李越. All rights reserved.
//

import UIKit
import SnapKit

class ComposeViewController: UIViewController {

    @objc private func close() {
        
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func send() {
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
    }

    private func setupUI() {
        
        setNavBar()
        setupTextView()
        setupToolBar()
    }
    
    private lazy var textView: UITextView = {
        
        let tv = UITextView()
        tv.font = UIFont.systemFontOfSize(18)
        tv.textColor = UIColor.darkGrayColor()
        //设置键盘消失
        tv.keyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
        //设置弹簧效果
        tv.alwaysBounceVertical = true
        return tv
    }()
    
    private lazy var placeHolderLabel: UILabel = UILabel(title: "分享新鲜事...", size: 18, color: UIColor.lightGrayColor())
    
    private lazy var toolBar: UIToolbar = UIToolbar()
}

extension ComposeViewController {
    
    private func setupToolBar() {
        
        view.addSubview(toolBar)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.left.bottom.right.equalTo(view)
            make.height.equalTo(40)
        }
        
        //设置
        var items = [UIBarButtonItem]()
        
        let itemSettings = [["imageName": "compose_toolbar_picture","actionName": "selectPicture"],
            ["imageName": "compose_mentionbutton_background"],
            ["imageName": "compose_trendbutton_background"],
            ["imageName": "compose_emoticonbutton_background"],
            ["imageName": "compose_add_background"]]
        
        for dict in itemSettings {
            
            let btn = UIButton(type: .Custom)
            let imageName = dict["imageName"]!
            btn.setImage(UIImage(named: imageName), forState: .Normal)
            btn.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
            let item = UIBarButtonItem(customView: btn)
            btn.sizeToFit()
            items.append(item)
            
            let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(space)
        }
        
        items.removeLast()
        toolBar.items = items
    }
    
    private func setupTextView() {
        
        view.addSubview(textView)
        textView.backgroundColor = UIColor.randomColor()
        textView.snp_makeConstraints { (make) -> Void in
            
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(view.bounds.height / 3)
        }
        
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textView.snp_top).offset(8)
            make.left.equalTo(textView.snp_left).offset(5)
        }
    }
    
    //设置导航条
    private func setNavBar() {
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "关闭", style: .Plain, target: self, action: "close")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发布", style: .Plain, target: self, action: "send")
        
        let titleView = UIView(frame: CGRectMake(0, 0, 200, 40))
        navigationItem.titleView = titleView
        
        //定义文案
        let titleLabel = UILabel(title: "发微博", size: 17, color: UIColor.darkGrayColor())
        let nameLabel = UILabel(title: UserAccountViewModel().userName ?? "", size: 14, color: UIColor.lightGrayColor())
        
        titleView.addSubview(titleLabel)
        titleView.addSubview(nameLabel)
        
        titleLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(titleView.snp_top)
            make.centerX.equalTo(titleView.snp_centerX)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(titleView.snp_bottom)
            make.centerX.equalTo(titleView.snp_centerX)
        }
    }
}

